//
//  HSYFMDBOperationInfo.m
//  高仿暴走斗图
//
//  Created by key on 16/10/11.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYFMDBOperationInfo.h"

@implementation HSYFMDBOperationFieldInfo

- (instancetype)initWithFieldName:(NSString *)name fieldType:(NSString *)type {
    
    if (self = [super init]) {
        _fieldName = name;
        _fieldType = type;
    }
    return self;
}

@end

@implementation HSYFMDBOperationInfo

- (instancetype)init {
    if (self = [super init]) {
        
        _fields = [NSMutableArray arrayWithCapacity:3];
        _statements = [NSMutableArray arrayWithCapacity:3];
    }
    return self;
}

- (NSString *)toDataBaseTableField {
    if (self.fields.count == 0) {
        return nil;
    }
    HSYFMDBOperationFieldInfo *firstInfo = [self.fields firstObject];
    NSString *field = [NSString stringWithFormat:@"%@ %@", firstInfo.fieldName, firstInfo.fieldType];
    for (NSInteger i = 1; i < self.fields.count; i ++) {
        HSYFMDBOperationFieldInfo *fieldInfo = self.fields[i];
        NSString *nextField = [NSString stringWithFormat:@"%@ %@", fieldInfo.fieldName, fieldInfo.fieldType];
        field = [NSString stringWithFormat:@"%@,%@", field, nextField];
    }
    return field;
}

- (NSString *)toDataBaseTableInsertStatements {
    
    NSMutableArray *new = [[NSMutableArray alloc] init];
    for (NSString *obj in self.statements) {
        [new addObject:[NSString stringWithFormat:@"'%@'", obj]];
    }
    return [HSYFMDBOperationInfo statementsForParams:new];
}

- (NSString *)toDataBaseTableInsertFields {
    
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    for (HSYFMDBOperationFieldInfo *info in self.fields) {
        [temp addObject:info.fieldName];
    }
    return [HSYFMDBOperationInfo statementsForParams:temp];
}

+ (NSString *)statementsForParams:(NSMutableArray <NSString *>*)params {
    if (params.count == 0) {
        return nil;
    }
    NSString *statement = [params firstObject];
    for (NSInteger i = 1; i < params.count; i ++) {
        statement = [NSString stringWithFormat:@"%@,%@", statement, params[i]];
    }
    return statement;
}

@end

@implementation HSYFMDBOperationInfo (Show)

+ (HSYFMDBOperationInfo *)createDataBaseTableForName:(NSString *)name fields:(NSMutableArray <HSYFMDBOperationFieldInfo *>*)fields {
 
    HSYFMDBOperationInfo *operationInfo = [[HSYFMDBOperationInfo alloc] init];
    operationInfo.name = name;
    operationInfo.fields = fields;
    return operationInfo;
}

+ (HSYFMDBOperationInfo *)createDataBaseTableForName:(NSString *)name fields:(NSMutableArray <HSYFMDBOperationFieldInfo *>*)fields insertDatas:(NSMutableArray <NSString *>*)statements {
    
    HSYFMDBOperationInfo *operationInfo = [[HSYFMDBOperationInfo alloc] init];
    operationInfo.name = name;
    operationInfo.statements = statements;
    operationInfo.fields = fields;
    return operationInfo;
}

@end
